package com.tianyu.leyou.exception;

import com.tianyu.leyou.model.ResultData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//后台全局异常拦截
@ControllerAdvice
public class ExceptionAdvice {

    //系统异常拦截
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultData exception(Exception e){
        ResultData resultData = new ResultData();
        resultData.setFlag(false);
        resultData.setMsg("请联系管理员");
        e.printStackTrace();
        return resultData;
    }

    //自定义异常拦截
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResultData exception(MyException e){
        ResultData resultData = new ResultData();
        resultData.setFlag(false);
        if (e.getMessage().equals("请登录")) {
            resultData.setCode("20001");
        }
        resultData.setMsg(e.getMessage());
        return resultData;
    }

}
